Google News
logo
JavaScript - Interview Questions
What is the usage of onpagehide event in JavaScript?
When a visitor leaves the web page, then the onpagehide event triggers. The visitor can move to another page or click a link after leaving the current web page. An example here displays an alert box when the user tries to leave the page. This happens since onpagehide event fires when the page is left : 
 
<!DOCTYPE html>
<html>
	<body onpagehide="display()">
	  <p>Close the page</p>
	  <script>
	     function display() {
    	     alert("It was nice having you here!");
	    }
	  </script>
	</body>
</html>
Advertisement